const express = require("express");
const Result = require("../../models/Result");
const { decoded } = require("../../utils/index");
const { PRIVATE_KEY, JWT_EXPIRED } = require("../../utils/constant");
const {
  getFixList,
  editFix,
  createFix,
  deleteFix,
  getTotalCount,
} = require("../../services/fix");
const jwt = require("jsonwebtoken");
const jwtAuth = require("../jwt");
// 注册路由
const router = express.Router();
router.use(jwtAuth);

//获取维修列表接口
router.get("/list", function (req, res, next) {
  const { pageNum, pageSize } = req.query;
  getFixList(req.query)
    .then((data) => {
      data.forEach((element) => {
        element.space = JSON.parse(element.space);
      });
      getTotalCount(req.query).then((total) => {
        new Result(
          {
            list: data,
            page: {
              pageNum,
              pageSize,
              total: total.totalCount,
            },
          },
          "维修列表查询成功"
        ).success(res);
      });
    })
    .catch((err) => {
      new Result("维修信息查询失败").fail(res);
    });
});

//编辑维修接口
router.post("/edit", function (req, res, next) {
  editFix(req.body)
    .then((data) => {
      new Result("操作成功").success(res);
    })
    .catch((err) => {
      console.log(err);
      new Result("操作失败").fail(res);
    });
});

//创建维修接口
router.post("/create", function (req, res, next) {
  createFix(req.body)
    .then((data) => {
      new Result("操作成功").success(res);
    })
    .catch((err) => {
      console.log(err);
      new Result("操作失败").fail(res);
    });
});

//删除维修接口
router.post("/delete", function (req, res, next) {
  deleteFix(req.body)
    .then((data) => {
      new Result("操作成功").success(res);
    })
    .catch((err) => {
      console.log(err);
      new Result("操作失败").fail(res);
    });
});

module.exports = router;
